<template>
  <el-container class="layout-container-demo">
    <el-aside width="200px">
      <el-scrollbar>
        <el-menu>
          <sidebarVue :menu="data.menuList"></sidebarVue>
        </el-menu>
      </el-scrollbar>
    </el-aside>
    <mainVue />
  </el-container>
</template>

<script lang="ts" setup>
import sidebarVue from "./sidebar.vue";
import mainVue from "./main.vue";
import { Menu as IconMenu, Message, Setting } from "@element-plus/icons-vue";
import { onMounted,reactive } from "vue";
import router from "@/router";
import { formatRoutes } from "@/utils/dealRouter";
import { getMenu } from "@/api/api";
onMounted(() => {
  menu();
});
type dataType={
  menuList:object[]
}
let data=reactive<dataType>({
  menuList:[]
})
const menu = async () => {
  // console.log('获取菜单');
  let res:any = await getMenu({ name: "admin" });
  // console.log(res?.menuList);
  if(res.menu.length!=0){
    data.menuList=res.menu
    // console.log(data.menuList,'menu');
    
  }
  localStorage.setItem("viteMenu", JSON.stringify(res?.menu));
  getRouter();
};
// const imgUrl = new URL('./img.png', import.meta.url)

// console.log(imgUrl.href,'imgUrl')
const getRouter = () => {
  let arr: Array<object> = JSON.parse(
    localStorage.getItem("viteMenu") as string
  );
  let route: Array<object> = formatRoutes(arr, true) as [];
  // console.log(route);
  // return
  route.forEach((item: any, idx: number) => {
    // console.log(idx);
    router.addRoute(item);
  });
  // console.log(router.getRoutes());
};
</script>

<style scoped>
</style>
